﻿(function ($, ka) {

	ka.view = ka.view || {};
	$.extend(ka.view, {
		$imageContent: null,
		$portfolioImageContainer: null,
		$orangeInfo: null
	});

	$.extend(ka, {

		initImageBox: function () {
			var view = ka.view;
			view.$imageContent = $('#imageContent');
			view.$portfolioImageContainer = $('#portfolioImageContainer');
			view.$orangeInfo = $('#orangeInfo');
			if (view.$imageContent.length) {
				view.$imageContent.magnificPopup({
					delegate: 'a',
					type: 'image',
					gallery: {
						enabled: true,
						navigateByImgClick: true,
						preload: [0,1]
					},
					image: {
						tError: '<a href="%url%">The image #%curr%</a> could not be loaded.'
					}
				});
			}

			if (view.$portfolioImageContainer.length) {
				var context = this;
				$.when(this.getPortfolioImages())
					.done(function (images) {
						context.showPortfolioImagePopup(images);
					});
			}
		},

		showPortfolioImagePopup: function (images) {
			var view = ka.view,
				items = $.map(images, function (url) {
				return { src: url };
			});
			view.$portfolioImageContainer.magnificPopup({
				items: items,
				type: 'image',
				gallery: {
					enabled: true,
					navigateByImgClick: true,
					preload: [0,1]
				},
				image: {
					tError: '<a href="%url%">The image #%curr%</a> could not be loaded.'
				}
			});
			if (view.$orangeInfo.length) {
				view.$orangeInfo.magnificPopup({
					items: [{ src: '/Content/images/orange_big.jpg' }],
					type: 'image',
					gallery: {
						enabled: true,
						navigateByImgClick: true,
						preload: [0,1]
					},
					image: {
						tError: '<a href="%url%">The image #%curr%</a> could not be loaded.'
					}
				});
			}
		},

		getPortfolioImages: function () {
			var url = "/Home/GetPortfolioImages";
			return $.ajax({
				method: 'GET',
				dataType: 'json',
				contentType: 'application/json; chrset=utf8',
				url: url,
				error: function(error) {
					console.log(error);
				}
			});
		}

	});

})(jQuery, konsolAntresol);